﻿
function Weather_Init()
{
		$("#weaterbox").mouseover(function(e){
			$("#weater1th").show();
			$("#weater2th").show();
		});
		
		$("#weaterbox").mouseout(function(e){
			$("#weater1th").hide();
			$("#weater2th").hide();				
		});
}

function Weather_MakeHtml(obj, num, day, Color)
{   
	text = "";
	code ="";
	display="display:none;"
	daily = "";
	if(num == 0){
		display = "";
		daily="今天";
	}
	if(num == 1){
		daily="明天";
	}
	if(num == 2){
		daily="后天";
	}
	if(day){
		text = obj.text_day;
		code =obj.code_day;
	}
	else{
		text = obj.text_night;
		code =obj.code_night;
	}
	tm = "" +obj.low + "-"+obj.high;
	wind_direction = obj.wind_direction;
	
	
	rv="\
		<div id='weater"+num+"th' style='position:absolute; top:20px;left:"+num*150+"px; width:150px;color:"+Color+";"+display+"'>\
			<span style='position:absolute; left:0px;color:"+Color+";font-size:12px'>"+daily+"</span>\
			<span style='position:absolute; left:70px;color:"+Color+"'>"+text+"</span><br>\
			<span style='position:absolute; left:70px;color:"+Color+"'>"+tm+"°C</span><br>\
			<span style='position:absolute; left:70px;color:"+Color+"'>"+wind_direction+"风</span>\
			<img style='position:absolute;left:0px;top:10px' src='img/3d_60/"+code+".png'/>\
		</div>"
	return rv;
}

function Weather_Refresh(Color)
{
		// 判断当前是白天还是晚上
		var date = new Date();
		var hours = date.getHours();
		var Day = true;
		if(hours <6 || hours > 18){
			Day = false;
		}
		
		// https://hellopage.purpleshine.cn:8080/weather/GetLocation
		LoadStroage_Location(function(location,context){
			var info = "https://hellopage.purpleshine.cn:8080/weather/GetWeagter?location="+location+"&jsoncallback=?";
			$.getJSON(info,
			function(data){
			  html ="<span style='color:"+Color+"'>"+location+"</span>"
			  html += Weather_MakeHtml(data.results[0].daily[0],0,Day, Color);
			  html += Weather_MakeHtml(data.results[0].daily[1],1,Day, Color);
			  html += Weather_MakeHtml(data.results[0].daily[2],2,Day, Color);
			  $("#weaterbox").html(html);
			  Weather_Init();
			});
				
		}, "");
}

$(function()
	{
		// 加载数据
		LoadStroage_Color(function(data, context){
			// 初始化界面
			Weather_Refresh(data);	
		}, "");

	});